<template>
  <div class="login-container">
    <header class="login-header">
      <img src="/images/17.png" alt="Pet Mall Logo" class="logo">
      <h1>Pet Mall 宠物商城</h1>
    </header>

    <main class="login-content">
      <div class="login-form">
        <h2>欢迎登录</h2>
        
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="username">用户名</label>
            <input 
              id="username"
              type="text" 
              v-model="username" 
              placeholder="请输入用户名"
              required
            >
          </div>

          <div class="form-group">
            <label for="password">密码</label>
            <input
              id="password"
              type="password"
              v-model="password"
              placeholder="请输入密码"
              required
            >
          </div>

          <div v-if="errorMessage" class="error-message">
            {{ errorMessage }}
          </div>

          <button 
            type="submit" 
            class="login-button"
            :disabled="isLoading"
          >
            <span v-if="!isLoading">登录</span>
            <span v-else>登录中...</span>
          </button>
        </form>

        <div class="register-link">
          <span>没有账号？</span>
          <button @click="handleRegister">立即注册</button>
        </div>
      </div>
    </main>

    <footer class="login-footer">
      <p>© 2024 Pet Mall 宠物商城</p>
    </footer>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '@/services/userService'
import notification from '@/utils/notification'

const router = useRouter()
const username = ref('')
const password = ref('')
const errorMessage = ref('')
const isLoading = ref(false)

const handleLogin = async () => {
  try {
    isLoading.value = true
    errorMessage.value = ''
    console.log('开始登录流程...')
    
    const res = await login(username.value, password.value)
    console.log('登录API返回:', res)
    
    localStorage.setItem('token', res.data.token)
    localStorage.setItem('username', username.value)
    console.log('本地存储设置完成')
    
    // 触发全局登录状态更新
    window.dispatchEvent(new CustomEvent('auth-change', {
      detail: { isLoggedIn: true, username: username.value }
    }))
    console.log('全局事件已触发')
    
    await router.push('/home')
    console.log('路由跳转完成')
  } catch (e) {
    console.error('登录错误:', e)
    errorMessage.value = e.response?.data?.message || '登录失败'
    notification({
      message: errorMessage.value,
      type: 'error'
    })
  } finally {
    isLoading.value = false
  }
}

const handleRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-header {
  padding: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background-color: rgba(255, 255, 255, 0.8);
}

.logo {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.login-header h1 {
  color: #42b983;
  font-size: 1.8rem;
  margin: 0;
}

.login-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.login-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-form h2 {
  color: #42b983;
  text-align: center;
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #666;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.form-group input:focus {
  outline: none;
  border-color: #42b983;
}

.error-message {
  color: #ff5252;
  text-align: center;
  margin: 1rem 0;
}

.login-button {
  width: 100%;
  padding: 0.75rem;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #3aa876;
}

.login-button:disabled {
  background-color: #a8d5c2;
  cursor: not-allowed;
}

.register-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #666;
}

.register-link button {
  background: none;
  border: none;
  color: #42b983;
  cursor: pointer;
  text-decoration: underline;
}

.login-footer {
  padding: 1rem;
  text-align: center;
  color: #666;
  background-color: rgba(255, 255, 255, 0.8);
}

@media (max-width: 768px) {
  .login-header {
    flex-direction: column;
    text-align: center;
  }
  
  .login-content {
    padding: 1rem;
  }
}
</style>
